// pages/dome/dome.js
const recorderManager = wx.getRecorderManager()
let garbageData = ""

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[
      '纸巾','安全帽','包包'
    ],
    result:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let garbageItem = new Array()
    this.data.list.forEach((item) => {
      this.init(item)
      .then((res) => {
        garbageItem.push(res[0])
        console.log(garbageItem);
      })
    })
    // console.log(garbageItem);
    
  },
  async init(text){
    let res = await wx.cloud.callFunction({
      name:'getGarbageSort'
    })
    // console.log(res);
    let searchResult = new Array(),categoryName = ""
    garbageData = res.result.data[0].Garbage_data
    garbageData.forEach((categoryItem) => {
      if(categoryItem && categoryItem.data && categoryItem.data.length > 0){
        // console.log(categoryItem);
        categoryItem.data.forEach((letterItem) => {
          let garbageItem = letterItem.garbageItem
          garbageItem.forEach((item) => {
            if(item == text){
              if(categoryItem.category == 1){
                categoryName = '可回收垃圾'
              }else if(categoryItem.category == 2){
                categoryName = '有害垃圾'
              }else if(categoryItem.category == 4){
                categoryName = '湿垃圾'
              }else if(categoryItem.category == 8){
                categoryName = '干垃圾'
              }
              searchResult.push({
                categoryName,
                garbageItem:item,
              })
            }
          })
        })
      }
    })
    return searchResult
  },
  handleTouchStart(){
    const options = {
      duration: 60000,//指定录音的时长，单位 ms
      sampleRate: 16000,//采样率
      numberOfChannels: 1,//通道数
      encodeBitRate: 48000,
      format:'aac'
    }
    recorderManager.start(options)
    // wx.showLoading({
    //   title: '正在识别中',
    // })
    recorderManager.onStop((res) => {
      console.log(res);
      const baiduAccessToken = wx.getStorageSync('access_token')
      console.log(baiduAccessToken);
      const fileSize = res.fileSize
      const tempFilePath = res.tempFilePath
      let format = 'pcm'
      if(tempFilePath){
        format = tempFilePath.substring(tempFilePath.lastIndexOf('.') + 1);
      }
      const fs = wx.getFileSystemManager()
      fs.readFile({
        filePath:tempFilePath,
        encoding:'base64',
        success:(res) => {
          const base64 = res.data
          // console.log(res.data);
          wx.request({
            url: 'https://vop.baidu.com/pro_api',
            method:'post',
            data:{
              format:format,
              rate:16000,
              channel:1,
              cuid:'baidu_workshop',
              token:'24.1d305143b752dc4ede9af1c594921543.2592000.1618040815.282335-23776776',
              dev_pid:80001,
              speech:base64,
              len:fileSize  
            },
            header:{
              'Content-Type':'application/json'
            },
            success:(res) => {
              console.log(res);
            }
            
          })
        }
      })
    })

  },
  handleTouchEnd(){
    recorderManager.stop()
  },
  stopEvent(){
    // recorderManager.onStop((res) => {
    //   console.log(res);
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})